package cn.lyvust.eais.api;


import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.evaluation.RecordAttachementParam;
import cn.lyvust.eais.manage.entity.EvaluationRecordAttachement;
import cn.lyvust.eais.manage.service.EvaluationRecordAttachementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author ggzheng
 * @since 2021-01-19
 */
@Api(tags = {"考核附件"})
@RestController
@RequestMapping("/evaluation/record_attachement")
public class EvaluationRecordAttachementController {
    @Autowired
    private EvaluationRecordAttachementService recordAttachementService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询考核附件")
    public ResponseResult list(@RequestBody RecordAttachementParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        if (!StringUtils.isEmpty(param.getRecordId())) {
            wrapper.eq("record_id", param.getRecordId());
        }
        if (!StringUtils.isEmpty(param.getTitle())) {
            wrapper.eq("title", param.getTitle());
        }
        wrapper.orderByDesc("create_date");
        return ResponseResult.e(ResponseCode.OK, recordAttachementService.list(wrapper));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个考核附件")
    @SysLogs("新增一个考核附件")
    public ResponseResult add(@RequestBody RecordAttachementParam param){
        if (StringUtils.isEmpty(param.getRecordId())) {
            throw RequestException.fail("添加失败,考核附件对应的记录主键Id不能为空!");
        }
        if (StringUtils.isEmpty(param.getTitle())) {
            throw RequestException.fail("添加失败,附件标题不能为空!");
        }
        if (StringUtils.isEmpty(param.getFileName())) {
            throw RequestException.fail("添加失败,附件文件存储路径不能为空!");
        }
        EvaluationRecordAttachement entity = new EvaluationRecordAttachement();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, recordAttachementService.saveOrUpdate(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个考核附件")
    @SysLogs("修改一个考核附件")
    public ResponseResult update(@RequestBody RecordAttachementParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,考核附件主键ID不能为空!");
        }
        EvaluationRecordAttachement entity = recordAttachementService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, recordAttachementService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个考核附件")
    @SysLogs("删除一个考核附件")
    public ResponseResult delete(@RequestBody RecordAttachementParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,考核附件主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, recordAttachementService.removeById(param.getId()));
    }
}
